#######################################################
# CreateNQCSDelivery.ps1
#
# Creates delivery packages for NQ Common Services
# using the given version string.
#
# Copyright (c) 2011-2012 Andreas Weizel
#######################################################

# Show help, if no args were given
if (($args.Length -lt 1) -or [String]::IsNullOrEmpty($args[0]))
{
	echo ''
	echo 'CreateNQCSDelivery.ps1 ReleaseVersion [-zn]'
	echo ''
	echo ' ReleaseVersion - The release version to use for file and directory names'
	echo '                  of the delivery.'
	echo ' -z             - Creates a ZIP file for the delivery'
	echo ' -n             - Creates a NuGet package for the delivery'
	echo ''
	echo ' Example:'
	echo ''
	echo ' This creates an NQCS_Binary_0.90.1234.zip in \DeploymentPackage directory.'
	echo ''
	echo '   ./CreateNQCSDelivery.ps1 0.90.1234 -z'
	echo ''
	exit
}

# Load configuration
./ToolConfig.ps1

$releaseVersion = $args[0]
$sourceBinDir = '../deploy/Release/bin'
$sourceDocDir = '../Documentation'
$destRootDir = '../DeploymentPackage/NQCS_' + $releaseVersion
$destBinDir = (join-path $destRootDir ('NQCS_' + $releaseVersion + '_Binary'))
$destSrcDir = (join-path $destRootDir ('NQCS_' + $releaseVersion + '_Source'))
$destNuGetDir = (join-path $destRootDir ('NQCS_' + $releaseVersion + '_NuGet'))

# Prepare output directories
echo 'Preparing directories ...'

if (test-path $destRootDir)
{
	rm $destRootDir -recurse -force
}

md $destBinDir > $null
md $destBinDir/Bin > $null
md $destBinDir/Documentation > $null
md $destSrcDir/NQCore > $null
md $destSrcDir/NQCS > $null
md $destSrcDir/NQsdk > $null
md $destSrcDir/Documentation > $null

# Copy delivery files
echo 'Copying files ...'

# Binary and documentation files
cp $sourceBinDir/NQCore.dll $destBinDir/Bin
cp $sourceBinDir/nq.exe* $destBinDir/Bin
cp $sourceBinDir/nqc.exe* $destBinDir/Bin
cp $sourceBinDir/LICENSE.txt $destBinDir
cp $sourceDocDir/*.pdf $destBinDir/Documentation
cp $sourceDocDir/HelpBuild/NQReference.chm $destBinDir/Documentation

cp $sourceBinDir/NQCommServ.Component.dll $destBinDir/Bin
cp $sourceBinDir/NQCommServ.Interface.dll $destBinDir/Bin
cp $sourceBinDir/NQCommServBasic.Component.dll $destBinDir/Bin
cp $sourceBinDir/NQCommServBasic.Interface.dll $destBinDir/Bin
cp $sourceBinDir/NQUpdater.exe $destBinDir/Bin
cp $sourceBinDir/NQsdk.Component.dll $destBinDir/Bin
cp $sourceBinDir/awzBoxes.dll $destBinDir/Bin
cp $sourceBinDir/AvalonDock.dll $destBinDir/Bin
cp $sourceDocDir/HelpBuild/NQCSReference.chm $destBinDir/Documentation

# Source files
cp ../CmdParams.txt $destSrcDir

cp ../Documentation/NQCSDoc.shfbproj $destSrcDir/Documentation

cp ../ExtLibraries $destSrcDir -recurse
cp ../NQCore/.nuget $destSrcDir/NQCore -recurse
cp ../NQCore/NQHost $destSrcDir/NQCore -recurse
cp ../NQCore/NQHostConsole $destSrcDir/NQCore -recurse
cp ../NQCore/NQServices $destSrcDir/NQCore -recurse
cp ../NQCore/NQServices.TestComponent1 $destSrcDir/NQCore -recurse
cp ../NQCore/NQServices.Tests $destSrcDir/NQCore -recurse
cp ../ToolScripts $destSrcDir -recurse

cp ../NQCS/NQ.CommonServices.nuspec $destSrcDir/NQCS
cp ../NQCS/Settings.txt $destSrcDir/NQCS
cp ../NQCS/NQCS.sln $destSrcDir/NQCS
cp ../NQCS/.nuget $destSrcDir/NQCS -recurse
cp ../NQCS/NQCommServ $destSrcDir/NQCS -recurse
cp ../NQCS/NQCommServBasic $destSrcDir/NQCS -recurse
cp ../NQCS/NQCommServBasic.Tests $destSrcDir/NQCS -recurse
cp ../NQCS/NQCommServBasicIfc $destSrcDir/NQCS -recurse
cp ../NQCS/NQCommServIfc $destSrcDir/NQCS -recurse
cp ../NQsdk/NQsdk $destSrcDir/NQsdk -recurse
cp ../NQCS/NQUpdater $destSrcDir/NQCS -recurse
cp ../awzBoxes $destSrcDir -recurse

# Remove unneeded files
rm $destSrcDir/ToolScripts/ToolConfig.*
ls $destSrcDir/* -include obj -recurse | rm -recurse

if (($args.Length -gt 1) -and $args[1].StartsWith('-'))
{
	# Create ZIP files if needed
	if ($args[1].IndexOf('z') -gt 0)
	{
		echo 'ZIPping files ...'

		# Binary ZIP file
		$binZIPFile = (join-path (gi $destRootDir).FullName "NQCS_Binary_$releaseVersion.zip")
		if (test-path $binZIPFile)
		{
			rm $binZIPFile
		}
		gi $destBinDir | ./out-zip.ps1 $binZIPFile
		
		# Source ZIP file
		$sourceZIPFile = (join-path (gi $destRootDir).FullName "NQCS_Source_$releaseVersion.zip")
		if (test-path $sourceZIPFile)
		{
			rm $sourceZIPFile
		}
		gi $destSrcDir | ./out-zip.ps1 $sourceZIPFile
	}
	
	# Create NuGet package if needed
	if ($args[1].IndexOf('n') -gt 0)
	{
		echo 'Creating NuGet package ...'
		
		# Build directory structure as NuGet convention prescribes
		md $destNuGetDir > $null
		md $destNuGetDir/lib/Net40 > $null
		md $destNuGetDir/content > $null
		md $destNuGetDir/content/awzhome > $null
		md $destNuGetDir/content/awzhome/binaries > $null
		md $destNuGetDir/content/awzhome/doc > $null
		
		cp ../NQCS/NQ.CommonServices.nuspec $destNuGetDir
		
		cp $destBinDir/Documentation/* $destNuGetDir/content/awzhome/doc
		
		cp $destBinDir/Bin/NQCommServ.Component.dll $destNuGetDir/content/awzhome/binaries
		cp $destBinDir/Bin/NQCommServBasic.Component.dll $destNuGetDir/content/awzhome/binaries
		cp $destBinDir/Bin/NQUpdater.exe $destNuGetDir/content/awzhome/binaries
		cp $destBinDir/Bin/NQsdk.Component.dll $destNuGetDir/content/awzhome/binaries
		
		cp $destBinDir/Bin/NQCommServBasic.Interface.dll $destNuGetDir/lib/Net40
		cp $destBinDir/Bin/NQCommServ.Interface.dll $destNuGetDir/lib/Net40
		
		if (($env:NuGetDir -ne $null) -and (test-path (join-path $env:NuGetDir NuGet.exe)))
		{
			$oldDir = (get-location)
			$destNuGetDirFull = (resolve-path $destNuGetDir)
			cd $env:NuGetDir
			./nuget pack (join-path $destNuGetDirFull NQ.CommonServices.nuspec) -BasePath $destNuGetDirFull -OutputDirectory $destNuGetDirFull/..
			cd $oldDir
		}
		else
		{
			echo 'ERROR: No NuGet command line tool found!'
		}
	}
}
